package cn.meizhu.pay.controller;

import cn.meizhu.base.canstant.OrderCanstant;
import cn.meizhu.order.domain.OrderAdopt;
import cn.meizhu.order.service.IOrderAdoptService;
import cn.meizhu.pay.domain.PayBill;
import cn.meizhu.pay.mapper.PayBillMapper;
import cn.meizhu.pay.service.IPayAliPayInfoService;
import cn.meizhu.pay.service.IPayBillService;
import cn.meizhu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Controller
@RequestMapping("/pay")
public class PayAliInfoController {

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IPayAliPayInfoService payAliPayInfoService;

    @Autowired
    private IOrderAdoptService orderAdoptService;


    @RequestMapping("/notifyUrl")
    @ResponseBody
    public String pay(HttpServletRequest  request){
        try {
            //获取支付宝POST过来反馈信息
            Map<String,String> params = new HashMap<String,String>();
            Map<String,String[]> requestParams = request.getParameterMap();
            for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
                String name = (String) iter.next();
                String[] values = (String[]) requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i]
                            : valueStr + values[i] + ",";
                }
                //乱码解决，这段代码在出现乱码时使用
                valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
                params.put(name, valueStr);
            }
            //我们自己系统的订单号
            String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
            //支付宝交易号
            String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
            //交易状态
            String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
            if (trade_status.equals("TRADE_SUCCESS")){
                PayBill payBill = payBillService.findByOrderSn(out_trade_no );
                payBill.setState(1);
                payBill.setUpdateTime(new Date());
                payBill.setUnionPaySn(trade_no);
                payBillService.update(payBill);
                switch (payBill.getBusinessType()){
                    case OrderCanstant.ORDER_ADOPT:
                        OrderAdopt orderAdopt = orderAdoptService.findById(payBill.getBusinessKey());
                        orderAdopt.setState(1);
                        orderAdopt.setPaySn(trade_no);
                        orderAdoptService.update(orderAdopt);
                        break;
                    case OrderCanstant.ORDER_GOODS:
                        break;
                    case OrderCanstant.ORDER_PRODUCT:
                        break;
                }
            }
            return "success";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "fail";
        }
    }


    @RequestMapping("/returnUrl")
    public String  returnUrl(){

        return "redirect:http://127.0.0.1/success.html";
    }
}
